// pages/spike/spike.js

var app = getApp()
Page({
  data: {
    aaa: 0,
    hiddens: true,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    currentPage0: "",
    currentPage1: "",
    currentPage2: "",
    currentPage3: "",
    circular:"true"
  },
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    /** 
     * 获取系统信息 
     */
    this.setData({
      current: options.id
    })
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    requestGroup(this, options.id)
  },
  eat: function () {//点击吃喝玩乐主菜单
    this.setData({
      current: 0
    })
    if (this.data.currentPage0 != 4) {
      requestGroup(this, 0)
    }

  },
  beauty: function () {//点击美容服饰主菜单
    this.setData({
      current: 1
    })
    if (this.data.currentPage1 != 1) {
      requestGroup(this, 1)
    }
  },
  life: function () {//点击教育生活主菜单
    this.setData({
      current: 2
    })
    if (this.data.currentPage2 != 2) {
      requestGroup(this, 2)
    }
  },
  home: function () {//点击建材家居主菜单
    this.setData({
      current: 3
    })
    if (this.data.currentPage3 != 3) {
      requestGroup(this, 3)
    }
  },
  toGroupDetail: function (e) {
    // console.log(e.target.dataset.id)
    wx.navigateTo({
      url: '../groupDetail/groupDetail?id=' + e.target.dataset.id + '&agentId=' + e.target.dataset.agentid,
    })
  },
  onReady: function () {
    // 页面渲染完成
    var that = this
    wx.request({//请求最近五天的秒杀详情
      url: app.globalData.allUrl + "/YMGW/seckillAction/getSeckillListByPostalcode",
      data: {
        postId: app.adcode
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
       // console.log(res.data)
        if (res.data) {
          var imgurls = [];
          for (var i = 0; i < res.data.length; i++) {
            imgurls.push({
              link: '/pages/spikeDetaile/spikeDetaile?id=' + res.data[i].id + "&agentId=" + res.data[i].agentId,
              url: 'https://www.ymg3.com/ymgImage/seckills/' + res.data[i].publicityImg
            })
          }
          that.setData({
            imgUrls: imgurls,
          })
        }
      }
    })
  },
  onShow: function (options) {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})

function requestGroup(that, id) {//根据不同的id来获取团购信息，0吃喝，1美容，2教育，3家具
  if (id == 0) {
    that.setData({
      currentPage0: 4
    })
  } else if (id == 1) {
    that.setData({
      currentPage1: 1
    })
  } else if (id == 2) {
    that.setData({
      currentPage2: 2
    })
  } else if (id == 3) {
    that.setData({
      currentPage3: 3
    })
  }
  wx.request({
    url: app.globalData.allUrl + "/YMGW/seckillAction/getSeckillProductByAgent",
    data: {
      classifyId: id,
      postId: app.adcode,
      pageNum: 1,
      pageCount: 10,
      status:5
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      //console.log(res.data)
      if (res.data&&res.data.list.length>0) {
        if (id == 0) {
          that.setData({
            groupLists0: res.data.list
          })
        } else if (id == 1) {
          that.setData({
            groupLists1: res.data.list
          })
        } else if (id == 2) {
          that.setData({
            groupLists2: res.data.list
          })
        } else if (id == 3) {
          that.setData({
            groupLists3: res.data.list
          })
        }
      }else{
        wx.showToast({
          title:"该区域暂无此类产品",
          icon:"loading"
        })
      }
    }
  })
}
